package com.apes.fico.engine.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fico.masterdata.gl.model.AccountingOperatingChart;
import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;

import com.apes.framework.util.ApesConst;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;


@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ReduceVoucherItem.CONST_TABLE_NAME)
public class ReduceVoucherItem implements ApesBean {
    public final static String CONST_TABLE_NAME = "gl_voucher_item_reduce";

    @Id
    @JSONField(serialize = false)
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "归约财务凭证分录", padding = 8)
    @Comment("编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "voucher_id", referencedColumnName = "id")
    @Comment("凭证内码")
    private ReduceVoucher voucher;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "acc_operating_chart_id", referencedColumnName = "id")
    @Comment("科目")
    private AccountingOperatingChart accountingOperatingChart;

    @Column(name = "debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("借方金额")
    private double debit = 0;

    public void changeDebit(double debit) {
        this.debit += debit;
    }

    @Column(name = "credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("贷方金额")
    private double credit = 0;

    public void changeCredit(double credit) {
        this.credit += credit;
    }

    @Comment("摘要")
    @Column(name = "summary", length = 400)
    private String summary;

    @OneToMany(mappedBy = "voucherItem", cascade = CascadeType.ALL) //, orphanRemoval = true
    private List<ReduceVoucherAuxiliary> voucherAuxiliaryList = new ArrayList<>();

    public List<ReduceVoucherAuxiliary> getVoucherAuxiliaryList() {
        if (voucherAuxiliaryList == null) return null;
        for (ReduceVoucherAuxiliary voucherAuxiliary : voucherAuxiliaryList) {
            voucherAuxiliary.setVoucherItem(this);
        }
        return voucherAuxiliaryList;
    }

    @Column(name = "aux", length = 32) //, nullable = false
    @Comment("辅助核算归集")
    private String aux;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "voucher_template_entry_id", referencedColumnName = "id")
    @Comment("模板分录")
    private VoucherTemplateEntry voucherTemplateEntry;


    public boolean checkUnique(String accountingOperatingChartId, String summary, String aux, String voucherTemplateEntryId) {
        if(!accountingOperatingChart.getId().equals(accountingOperatingChartId)) return false;
        if(!isEqualMayNull(this.summary,summary,true)) return  false;
        if(!isEqualMayNull(this.aux,aux,true)) return false;
        if(!isEqualMayNull(this.voucherTemplateEntry, voucherTemplateEntryId,false) || !isEqualMayNull(this.voucherTemplateEntry.getId(),voucherTemplateEntryId,true)) return false;
        return true;
    }

    public boolean isEqualMayNull(Object str1, Object str2, boolean isStr) {
        if((StringUtils.isEmpty(str1) && !StringUtils.isEmpty(str2)) || (!StringUtils.isEmpty(str1) && StringUtils.isEmpty(str2))) return false;
        if(isStr && !StringUtils.isEmpty(str1) && !str1.equals(str2)) return false;
        return true;
    }



}
